package com.app.miwawa.api.system.service.bo;

import java.util.List;

import javax.annotation.Resource;

import org.framework.common.api.entity.CommonEntityAware;
import org.framework.common.api.query.Criterion.Operator;
import org.framework.common.api.service.bo.CommonBoAdapter;
import org.framework.common.api.service.dao.CommonDaoAware;
import org.framework.common.api.support.constant.SystemConstants;
import org.framework.common.api.support.model.PageModel;
import org.framework.common.api.support.model.QueryModel;
import org.framework.common.api.support.model.TreeModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.app.miwawa.api.system.entity.Assets;
import com.app.miwawa.api.system.entity.Auths;
import com.app.miwawa.api.system.service.dao.AuthsDao;

/**
 * 业务接口
 * <p>描述: 系统资源－业务接口</p>
 * <p>Company: Professional</p>
 * @author chanlong(陈龙)
 * @date 2016年1月23日 上午9:37:10
 * @version 1.0.2016
 */
@Service("assetsBo")
public class AssetsBo extends CommonBoAdapter<Assets, String> {

	@Resource(name = "defaultDao")
	private CommonDaoAware<CommonEntityAware<String>, String> dao;
	
	@Autowired
	private AuthsDao authsDao;
	
	/**
	 * @see org.framework.common.api.service.bo.CommonBoAdapter#tree(org.framework.common.api.support.model.QueryModel)
	 * @author chanlong(陈龙)
	 * @param groupsid 
	 * @date 2016年4月1日 下午2:19:43
	 */
	public TreeModel[] tree(final String groupsid, final QueryModel model) throws Exception {
		List<?> result = dao.findByQuery(model).getRows();
		
		return TreeModel.create(result, getAuths(groupsid));
	}
	
	/**
	 * 删除绑定资源.
	 * @param groupsid
	 * @author chanlong(陈龙)
	 * @date 2016年4月1日 下午5:32:39
	 */
	public void deleteAuths(final String groupsid){
		authsDao.delete(groupsid);
	}
	
	private String[] getAuths(final String groupsid) throws Exception{
		QueryModel query = new QueryModel(Auths.class, 0, 0);
		query.addAndQuery("groupsid", groupsid, Operator.EQ);
		query.addAndQuery("status", SystemConstants.DICT_STATUS_DEFAULT, Operator.EQ);
		query.addAndQuery("invalid", SystemConstants.DICT_DELETE_INVALID, Operator.EQ);
		query.addOrder("assetsCode", false);
		
		String[] assetsids = null;
		PageModel<?> result = authsDao.findByQuery(query);
		if(result != null){
			List<Auths> auths = result.getRows(Auths.class);
			
			int size = auths.size();
			assetsids = new String[size];
			
			for (int i = 0; i < size; i++) {
				assetsids[i] = auths.get(i).getAssetsid();
			}
		}
		return assetsids;
	}
}
